unit clsMan_Maquina;

interface

uses Dm,dbtables,biblio;

type TMan_Maquina =class(TObject)
  public
    Qtde : double;
    Valor: double;
    procedure Recuperar(Cod_Maquina: string; Data_Mov: TDateTime; Cod_HistMaq: string);
    procedure Gravar   (Cod_Maquina: string; Data_Mov: TDateTime; Cod_HistMaq: string);
    procedure Excluir  (Cod_Maquina: string; Data_Mov: TDateTime; Cod_HistMaq: string);
  end;

implementation

procedure TMan_Maquina.Excluir;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Delete from Man_Maquina where Cod_Maquina=:a and Data_Mov=:b and '+
            'Cod_HistMaq=:c');
 tq.params[0].asstring  := Cod_Maquina;
 tq.params[1].asdatetime:= Data_Mov;
 tq.params[2].asstring  := Cod_HistMaq;
 tq.execsql;
 tq.close;
 tq.free;
end;

procedure TMan_Maquina.Gravar;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from Man_Maquina where Cod_Maquina=:a and Data_Mov=:b and '+
            'Cod_HistMaq=:c');
 tq.params[0].asstring  := Cod_Maquina;
 tq.params[1].asdatetime:= Data_Mov;
 tq.params[2].asstring  := Cod_HistMaq;
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Insert into Man_Maquina values(:k_Cod_Maquina,:k_Data_Mov,:k_Cod_HistMaq,'+
             ':k_Qtde,:k_Valor)');
 end else begin
  tq.close;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Update Man_Maquina set Qtde=:k_Qtde,Valor=:k_Valor where '+
             'Cod_Maquina=:k_Cod_Maquina and Data_mov=:k_Data_Mov and '+
             'Cod_HistMaq=:k_Cod_HistMaq');
 end;
 tq.parambyname('k_Cod_Maquina').asstring:= Cod_Maquina;
 tq.parambyname('k_Data_Mov').asdatetime := Data_Mov;
 tq.parambyname('k_Cod_HistMaq').asstring:= Cod_HistMaq;
 tq.parambyname('k_Qtde').asfloat        := Qtde;
 tq.parambyname('k_Valor').asfloat       := Valor;
 tq.execsql;
 tq.close;
 tq.free;
end;

procedure TMan_Maquina.Recuperar;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from Man_Maquina where Cod_Maquina=:a and Data_Mov=:b and '+
            'Cod_HistMaq=:c');
 tq.params[0].asstring  := Cod_Maquina;
 tq.params[1].asdatetime:= Data_Mov;
 tq.params[2].asstring  := Cod_HistMaq;
 tq.open;
 Qtde := tq.fieldbyname('Qtde').asfloat;
 Valor:= tq.fieldbyname('Valor').asfloat;
 tq.close;
 tq.free;
end;

end.
